from django.shortcuts import render
from .serializers import SKUIndexSerializer
from django_filters.rest_framework import filters
from rest_framework.generics import GenericAPIView, ListAPIView
from .models import SKU
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.response import Response
from . import serializers
class HotSkuListView(ListAPIView):
    #关闭分页
    pagination_class = None
    serializer_class = serializers.HotSkuListSerializer
    def get_queryset(self):
        category_id = self.kwargs.get("category_id")
        skus = SKU.objects.filter(category_id = category_id).order_by("-sales")[:3]

        # def get(self,request,category_id):
        # 1,获取参数
        # queryset = self.get_queryset()
        # 2，获取序列化器
        # serializer = self.get_serializer(instance=queryset,many=True)
        # 3，返回数据
        # return Response(serializer.data)

class SkuListView(ListAPIView):
    serializer_class = serializers.HotSkuListSerializer
    filter_backends = (filters.OrderingFilter,)
    ordering_fields = ('create_time','price','sales')
    def get_queryset(self):
        #1,获取分类编号
        category_id = self.kwargs.get("category_id")

        #2,根据分类编号查询数据
        skus =SKU.objects.filter(category_id = category_id)

        return skus



class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer